<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="套餐名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入套餐名"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              type="daterange"
              :shortcuts="defaultShortcuts"
              :disabled-date="disabledDate"
              value-format="YYYY-MM-DD HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
            >
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['system:tenant-package:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" />
              新增
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <PureTableBar title="" :columns="columns" @refresh="handleQuery">
      <template #default="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          align-whole="center"
          :loading="loading"
          :size="size"
          :data="list"
          :columns="dynamicColumns"
        >
          <template #type="scope">
            <dict-tag :type="DICT_TYPE.TENANT_IDENTITY" :value="scope.row.type" />
          </template>
          <template #operation="scope">
            <div class="flex items-center justify-center">
              <el-button
                link
                type="primary"
                @click="
                  openForm(
                    scope.row.isShowUpdateAndDeleteButton ? 'update' : 'preview',
                    scope.row.id
                  )
                "
                v-hasPermi="['system:tenant-package:update']"
              >
                {{ scope.row.isShowUpdateAndDeleteButton ? '修改' : '查看' }}
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['system:tenant-package:delete']"
                v-if="scope.row.isShowUpdateAndDeleteButton"
              >
                删除
              </el-button>
            </div>
          </template>
          <template #empty>
            <Empty />
          </template>
        </pure-table>
        <Pagination
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNo"
          :total="total"
          @pagination="getList"
        />
      </template>
    </PureTableBar>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <TenantPackageForm ref="formRef" @success="getList" />
  <!-- 总部端 套餐 表单弹窗：添加/修改 目前不用-->
  <!-- <TenantPackageFormHq ref="formRef" @success="getList" v-if="tenantTypeValue == 3" /> -->
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as TenantPackageApi from '@/api/system/tenantPackage'
import TenantPackageForm from './TenantPackageForm.vue'
// import TenantPackageFormHq from './TenantPackageFormHq.vue'
import { PureTableBar } from '@/components/RePureTableBar'
const tableRef = ref()
import { useTable } from './utils/hook'
const { columns } = useTable()
defineOptions({ name: 'SystemTenantPackage' })
import { defaultShortcuts } from '@/utils/formatTime'
/**时间范围限制 */
const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  status: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TenantPackageApi.getTenantPackagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TenantPackageApi.deleteTenantPackage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(async () => {
  getList()
})
</script>
